<?php
include_once('config/config.php');

// 验证登录
$admin = LoginCheck();

// 通过get拿到来时传递的数据
$name = $_GET['name'] ?? 0;

// 拿到数据库全部数据
$department = find("SELECT * FROM `pre_department` WHERE `name` = '$name'");

// 验证
if (!$department) {
  notice('部门不存在');
}

// 验证上传的表单数据
if ($_POST) {

  // 拿到表单数据
  $newName = isset($_POST['name']) ? trim($_POST['name']) : '';

  // 验证表单数据
  if (empty($newName)) {
    notice('名称不能为空');
  }
  if ($newName == $department['name']) {
    notice('名称不能重复');
  }

  // 封装数据
  $data = [
    'name' => $newName,
  ];

  // 更新数据
  $result = update('department', $data, "`name` = '$name'");

  if ($result['code'] === 1) {
    notice('修改成功', 'departmentList.php');
  }
  echo $result['msg'];
  exit;
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
  <?php include_once('common/meta.php'); ?>
  <link rel="stylesheet" href="/assets/lib/city-picker/city-picker.css">

  <script src="/assets/lib/city-picker/city-picker.data.min.js"></script>
  <script src="/assets/lib/city-picker/city-picker.min.js"></script>
</head>
<style>
  #upload {
    width: 200px;
    height: 200px;
    object-fit: cover;
  }
</style>

<body>
  <?php include_once('common/header.php'); ?>

  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">部门修改</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">PersonAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">

        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='PersonList.php'"><i class="icon-list"></i>
            部门列表
          </button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <label>部门名称</label>
                <input type="text" name="name" value="<?php echo $department['name']; ?>" class="input-xxlarge"><br>
                <input class="btn btn-primary" type="submit" value="修改部门" />
              </form>
            </div>
          </div>
        </div>

        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>

</body>

</html>